<?php


namespace App\Http\Controllers\Api\Repositories;


use App\Dengje\Utils\Settings;
use App\Models\Posts\WxComment;
use App\Models\Posts\WxPost;
use App\Models\WxCommentLike;
use App\Models\WxNotice;
use App\Models\WxUser;
use Dcat\Admin\Form\Field\Nullable;


class WxCommentRepositores
{

    /**
     * @param $posts_id 文章id
     * @param $comment_id 评论id
     * @param $user_id 评论的用户id
     * @param $comment_content 评论内容
     */
    public static function add($posts_id, $comment_id, $reply_user_id, $user_id, $comment_content, $comment_img_url)
    {
        $posts = WxPost::where('id', $posts_id)->first();
        $user = WxUser::where('id', $user_id)->first();
        $agentUser = WxUser::where('id', $reply_user_id)->first();

        if (!empty($comment_id)) {
            $comment = WxComment::where('id', $comment_id)->first();
            $CommentModel = new WxComment();
            $CommentModel->posts_id = $posts_id;
            $CommentModel->posts_user_id = $posts['user_id'];
            $CommentModel->user_id = $user_id;
            $CommentModel->user_name = $user['user_name'];
            $CommentModel->user_avatar = $user['user_avatar'];
            $CommentModel->comment_agent_id = $agentUser['id'];
            $CommentModel->comment_agent_name = $agentUser['user_name'];
            $CommentModel->comment_agent_avatar = $agentUser['user_avatar'];
            $CommentModel->comment_id = $comment_id;
            $CommentModel->comment_img_url = $comment_img_url;
            $CommentModel->comment_content = $comment_content;
            // 判断是否需要审核评论
            if (Settings::get('is_audit_comment') == 1) {
                $CommentModel->comment_state = 1;
            }
            $CommentModel->save();

            (new WxNotice())->addNotice($reply_user_id, '你的评论收到了一个回复快去看看吧', $user['user_name'] . '回复了您的评论。', 4, $posts_id);

        } else {

            $CommentModel = new WxComment();
            $CommentModel->posts_id = $posts_id;
            $CommentModel->posts_user_id = $posts['user_id'];
            $CommentModel->user_id = $user_id;
            $CommentModel->user_name = $user['user_name'];
            $CommentModel->user_avatar = $user['user_avatar'];
            $CommentModel->comment_content = $comment_content;
            $CommentModel->comment_img_url = $comment_img_url;
            // 判断是否需要审核评论
            if (Settings::get('is_audit_comment') == 1) {
                $CommentModel->comment_state = 1;
            }
            $CommentModel->save();

            (new WxNotice())->addNotice($posts['user_id'], '收到了一个评论快去看看吧', $user['user_name'] . '评论了您的动态。', 4, $posts_id);

        }

    }

    /**
     * 通过帖子ID获取评论列表
     * @param $posts_id
     * @param $limit
     * @return mixed
     */
    public static function getCommentByPostsId($posts_id, $field, $uid = 0, $limit = 10)
    {
        $data = WxComment::where('comment_state', 1)->where('posts_id', $posts_id)
            ->where('comment_id', null)
            ->simplePaginate($limit, $field);
        $data->append(['format_time']);
        $data->map(function ($item) use ($uid) {
            $imgList = array();
            array_push($imgList, ['img_url' => $item->comment_img_url]);
            $item->uid = $uid;
            $item->is_like = WxCommentLike::where('comment_id', $item->id)->where('user_id', $uid)
                ->exists();
            $item->like_count = WxCommentLike::where('comment_id', $item->id)->count();
            $item->imgList = $imgList;
            $childData = WxComment::where('comment_id', $item->id)->get(['id', 'user_name', 'comment_content', 'user_id', 'posts_user_id', 'user_avatar', 'comment_img_url', 'created_at', 'comment_agent_name', 'comment_agent_id']);
            $childData->map(function ($item) use ($uid) {
                $imgList = array();
                array_push($imgList, ['img_url' => $item->comment_img_url]);
                $item->is_like = WxCommentLike::where('comment_id', $item->id)->where('user_id', $uid)->exists();
                $item->like_count = WxCommentLike::where('comment_id', $item->id)->count();
                $item->imgList = $imgList;
            });
            $childData->append(['format_time']);
            $item->child = $childData;
        });
        return $data;
    }


    /**
     * 获取评论数量
     */
    public static function commentCount($postId)
    {

        return WxComment::where(['posts_id' => $postId, 'comment_state' => 1])->count();
    }
}
